package com.example.demo.use_demo.a_shiro;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 配置类
 * @author zhangqiang
 * @version 1.0
 * @date 2025/02/08
 */
@Configuration
public class ShiroConfig {

    @Bean
    public AccountRealm accountRealm(){
        return new AccountRealm();
    }

    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("accountRealm") AccountRealm accountRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(accountRealm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 权限设置authc/perms/roles...
        Map<String,String> map = new HashMap<>();
        map.put("/account/main","authc");
        map.put("/account/manage","perms[manage]");
        map.put("/account/administrator","roles[administrator]");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        // 设置登录页面
        shiroFilterFactoryBean.setLoginUrl("/account/login");
        // 设置未授权页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/account/unauth");
        return shiroFilterFactoryBean;
    }

/*    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }*/
}
